<?php

class Admin_goodsController extends Admin_baseController {


	public function listAction() {

		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$name = Request::getParams('name');
		$categoryId = Request::getParams('categoryId');

		$where = $value = $url = array();
		if ($name) {
			$where[] = 'goods_name like ?';
			$value[] = '%' . $name . '%';
			$url['name'] = $name;
		}
        if ($categoryId) {
            $where[] = 'goods_categral = ?';
            $value[] = $categoryId;
            $url['goods_categral'] = $categoryId;
        }


		$total = self::model('integral_goods')->count(null, $where, $value);
		$list = self::model('integral_goods')->limit(($page - 1) * $pageSize, $pageSize)->order('goodsId desc')->getAll($where, $value);



		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'name' => $name,
			'total' => $total,
			'list' => $list,
			'categoryId' => $categoryId,
			'categoryList' => self::model('integral_categral')->getAll("is_delete=?",0),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_goods/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

    public function editAction(){
	    $goodsId=Request::getParams("goodsId");
	    $data["goods_image"]=Request::getParams("goods_image");
        $data["integral"]=Request::getParams("integral");
        $data["sales_num"]=Request::getParams("sales_num");
        $data["stock_num"]=Request::getParams("stock_num");
        $data["goods_desc"]=Request::getParams("goods_desc");
        $data["goods_categral"]=Request::getParams("goods_categral");
        $data["is_recommend"]=Request::getParams("is_recommend");
        $data["user_level"]=Request::getParams("user_level");
        $data["buy_num"]=Request::getParams("buy_num");
        if($goodsId&&!$info = self::model('integral_goods')->getOne('goodsId=?', $goodsId)){
            Response::showMsg('信息查询失败，请稍后重试');
        }
	    if(Request::isGet()){
        $this->assign([
            "info"=>$info,
            "user_level"=>[
                ["id"=>1,"level_name"=>"青铜"],
                ["id"=>2,"level_name"=>"青铜Ⅲ"],
                ["id"=>3,"level_name"=>"青铜Ⅱ"],
                ["id"=>4,"level_name"=>"青铜Ⅰ"],
                ["id"=>5,"level_name"=>"白银Ⅱ"],
                ["id"=>6,"level_name"=>"白银Ⅰ"],
                ["id"=>7,"level_name"=>"黄金"],
                ["id"=>8,"level_name"=>"铂金"],
                ["id"=>9,"level_name"=>"钻石"]
                ],
            'categoryList' => self::model('integral_categral')->getAll("is_delete=?",0),
        ]);
        $this->display();
        exit;
        }

        if(empty($data["goods_image"])){
            Response::showMsg('请上传商品图');
        }
        if(empty($data["integral"])){
            Response::showMsg('请输入所需积分');
        }
        if(empty($data["goods_desc"])){
            Response::showMsg('请输入商品详情');
        }
        if(empty($data["goods_categral"])){
            Response::showMsg('请选择商品分类');
        }
        $data["update_time"]=time();
        if($goodsId){
            self::model("integral_goods")->update($data,"goodsId=?",$goodsId);
            Response::ajax(200, '修改成功');
        }else{
            $data["add_time"]=time();
            self::model("integral_goods")->insert($data);
            Response::ajax(200, '添加成功');
        }

    }

}